﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using OnlineCourse.Model;
using OnlineCourse.Model.Entity;

namespace OnlineCourse.Services
{
    public class UserService : IServices.IUserService
    {
        private readonly ApplicationDbContext _db;
        public UserService(ApplicationDbContext db)
        {
            this._db = db;
        }/// <summary>
         /// 添加用户
         /// </summary>
         /// <param name="Name"></param>
         /// <param name="pwd"></param>
         /// <param name="Sex"></param>
         /// <param name="Age"></param>
         /// <param name="Account"></param>
         /// <returns></returns>
        public bool Add(string Name, string pwd, string Sex, int Age)
        {
            try
            {
                User u = new User();
                u.UserName = Name;
                u.Password = pwd;
                u.Sex = Sex;
                u.Age = Age;
                _db.Users.Add(u);
                _db.SaveChanges();
                return true;
            }
            catch (Exception error)
            {

                throw error;
            }
        }
        public bool Delete(Guid id)
        {
            try
            {
                var m = _db.Users.FirstOrDefault(p => p.Id == id);
                _db.Users.Remove(m);
                _db.SaveChanges();
                return true;
            }
            catch (Exception err)
            {

                throw err;
            }

        }
        public User GetById(Guid id)
        {
            var m = _db.Users.FirstOrDefault(p => p.Id == id);
            return m;
        }

        public User GetByLogin(string name, string pwd)
        {
            var m = _db.Users.FirstOrDefault(p => p.UserName == name && p.Password == pwd);
            return m;
            
        }
        public IList<User> GetList(out int total, int page = 1, int pageSize = 10)
        {
            total = 0;
            var list = _db.Users.AsQueryable();
            total = list.Count();
            list = list.Skip(pageSize * (page - 1)).Take(pageSize);
            return list.ToList();
        }

        public bool Update(Guid id, User model)
        {
            try
            {
                var m = _db.Users.FirstOrDefault(p => p.Id == id);
                if (!string.IsNullOrEmpty(model.UserName))
                {
                    m.UserName = model.UserName;
                }
                if (!string.IsNullOrEmpty(model.HeadImage))
                {
                    m.HeadImage = model.HeadImage;
                }
                if (!string.IsNullOrEmpty(model.Password))
                {
                    m.Password = model.Password;
                }
                if (model.Age > 0)
                {
                    m.Age = model.Age;
                }
                if (!string.IsNullOrEmpty(model.Sex))
                {
                    m.Sex = model.Sex;
                }
                if (model.Account != 0)
                {
                    m.Account = model.Account;
                }

                m.UpdatedDate = DateTime.Now;
                _db.SaveChanges();

                return true;
            }
            catch (Exception err)
            {

                throw err;
            }
        }
        
    }
}
